﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Denting
{
    public class Elements:List<Element>
    {
        public Elements GetElementsFromInputFile(string inpFullFileName,int componentID)
        {
            string componentName="";
            foreach(HMComponent cp in Form1.ListHMComponent)
            {
                if (cp.compID==componentID)
                {
                    componentName = cp.compName;
                }
            }
            string tempInputFile = inpFullFileName + ".david";
            this.Clear();
            File.Copy(inpFullFileName, tempInputFile);
            StreamReader sr = new StreamReader(tempInputFile);

            string s = sr.ReadLine();
            string[] sArray;
            bool canReadElement = false;
            while (s != null)
            {
                if (canReadElement)
                {
                    if (s.Length >0 && s[0] == '*')
                    {
                        canReadElement = false;
                    }
                    sArray = s.Split(',');
                    if (sArray.Length==4)
                    {
                        Element e =new Element(Convert.ToInt32(sArray[0]), Convert.ToInt32(sArray[1]), Convert.ToInt32(sArray[2]), Convert.ToInt32(sArray[3]));
                        this.Add(e);
                    }
                    if (sArray.Length == 5)
                    {
                        Element e = new Element(Convert.ToInt32(sArray[0]), Convert.ToInt32(sArray[1]), Convert.ToInt32(sArray[2]), Convert.ToInt32(sArray[3]),Convert.ToInt32(sArray[4]));
                        this.Add(e);
                    }
                }
                if (s.Length >= 8 && s.Substring(0, 8) == "*ELEMENT" && s.Contains(componentName))
                {
                    canReadElement = true;
                }
                s = sr.ReadLine();
            }
            sr.Close();
            File.Delete(tempInputFile);
            return this;
        }

        public Elements FindElementsAttatchToNodes(Nodes MyNodes)
        {
            HashSet<Element> setElement = new HashSet<Element>();
            Elements tempElements = new Elements();

            foreach (Node n in MyNodes)
            {
                foreach (Element e in this)
                {
                    if (e.Node_1 == n.NodeID || e.Node_2 == n.NodeID || e.Node_3 == n.NodeID || e.Node_4 == n.NodeID)
                    {
                        setElement.Add(e);
                    }
                }
            }
            foreach (Element e in setElement)
            {
                tempElements.Add(e);
            }
            return tempElements;
        }

        public Elements FindElementsOnOneNode(int nodeID)
        {
            Elements tempElements = new Elements();
            foreach (Element e in this)
            {
                if (e.Node_1 == nodeID || e.Node_2 == nodeID || e.Node_3 == nodeID || e.Node_4 == nodeID)
                {
                    if (!tempElements.ContainsElementID(e.ElementID))
                    {
                        tempElements.Add(e);
                    }
                }
            }

            return tempElements;
        }

        public bool ContainsElementID(int elementID)
        {
            foreach (Element e in this)
            {
                if (e.ElementID == elementID)
                    return true;
            }
            return false;
        }

        public bool ContainsNodeID(int nodeID)
        {
            foreach (Element e in this)
            {
                if (e.Node_1 == nodeID ||e.Node_2==nodeID||e.Node_3==nodeID||e.Node_4==nodeID)
                    return true;
            }
            return false;
        }

        public Elements CombinElements(Elements es)
        {
            foreach (Element e in es)
            {
                if (!this.ContainsElementID(e.ElementID))
                {
                    this.Add(e);
                }
            }
            return this;
        }

        public Elements ExceptElements(Elements es)
        {
            for (int i = this.Count-1; i>=0;i-- )
            {
                if (es.ContainsElementID(this[i].ElementID))
                {
                    this.RemoveAt(i);
                }
            }
            return this;
        }

        public Element FindElementUseTwoNode(int node1, int node2)
        {
            Element tempElement = null;
            Elements es = FindElementsOnOneNode(node1);
            foreach (Element e in es)
            {
                if (e.Node_1==node1 &&e.Node_2==node2)
                {
                    tempElement = e;
                }
                if (e.Node_2 == node1 && e.Node_3 == node2)
                {
                    tempElement = e;
                }
                if (e.IsElementTriangle)
                {
                    if (e.Node_3 == node1 && e.Node_1 == node2)
                    {
                        tempElement = e;
                    }
                } 
                else
                {
                    if (e.Node_3 == node1 && e.Node_4 == node2)
                    {
                        tempElement = e;
                    }
                    if (e.Node_4 == node1 && e.Node_1 == node2)
                    {
                        tempElement = e;
                    }
                }
            }
            return tempElement;
            
        }

        public Elements CloneFromElements(Elements es)
        {
            this.Clear();
            foreach (Element e in es)
            {
                this.Add(e.clone());
            }
            return this;
        }

    }
}
